from django.shortcuts import render,HttpResponse
# Create your views here.


def login(request):
	return HttpResponse('Login')


def register(request):
	return HttpResponse('register')


def upload(request):
	import os,json
	ret = {'status':False,'data':None,'errMessage':None}
	if request.method == 'POST':
		try:
			title = request.POST.get('imgtitle',None)
			fileobj = request.FILES.get('fileobj',None)  #对应form表单的name
			if fileobj:
				filepath = os.path.join('static',fileobj.name)
				f = open(filepath,'wb')
				for chunk in fileobj.chunks():
					f.write(chunk)
				f.close()
				ret['status'] = True
				ret['data'] = filepath
			else:
				ret['errMessage'] = '文件上传失败！'
		except Exception as e :
			ret['errMessage'] = str(e)
		return HttpResponse(json.dumps(ret))
	else:
		return render(request,'upload.html',locals())
